// pages/test/index.js
import {
  request
} from '../../request/index.js'
Page({
  data: {
    // 题目数据
    infordata: [],
    // 按钮的点击变化
    color: '',
    // 题目的答案
    answer: 0,
    // 选中的答案
    option: -1,
    //  下一题判断
    answeroption: -1,
    //解析的显示与隐藏
    show: false,
    // 正确的题数数
    truenum: 0,
    //写的题目数
    writenum: 0,
    // 题目的索引
    infordataindex: 0,
    // 题目长度
    infordatalength: 0,
    // 记录正确题数的关键
    times: 1,
    // 记录是否做过题目
    doneinfor: 0,
    // 记录收藏的题目
    collectinfor: [],
    // 收藏的题目数
    collectindex: 0,
    // 收藏图标改变
    isCollect: false,
    // 加入错题
    errorinfor: [],
    // 加入错题判断
    iserrorcollect: false,
    // subject
    subject: 0,
    model: 0,
    // 是否发送请求
    isrequest: false,
    showpage: false,
    // 按键禁用
    stopnext: false

  },

  onLoad: function (options) {

    console.log(options);
    let subject = options.subject
    let isrequest = subject;
    let model = options.model
    if (subject != this.data.subject) {
      let isrequest = true
      this.setData({
        isrequest
      })
    } else {
      let isrequest = false
      this.setData({
        isrequest
      })
    }
    this.setData({
      subject,
      model,
      isrequest
    })

    this.getInforData();
  },
  // 获取题目
  async getInforData() {
    const infordata = wx.getStorageSync('errorinfor') || [];
    if (infordata.length > 0) {
      this.setData({
        showpage: true
      })
    }


    // 获取题目长度 console.log(infordata.length);
    const infordatalength = infordata.length;
    let collectinfor = wx.getStorageSync('collectinfor') || [];
    let isCollect = collectinfor.some(v => v.id == infordata.id);
    let errorinfor = wx.getStorageSync('errorinfor') || [];
    let iserrorcollect = errorinfor.some(v => v.id == infordata.id);
    let isrequest = this.data.isrequest
    if (infordata) {
      this.setData({
        infordata,
      })

    } else {
      this.setData({
        infordata: this.data.result,
        answer: 0,
        option: -1,
        show: false,
        truenum: 0,
        writenum: 0,
        collectinfor,
        isCollect,
        iserrorcollect
      })
      // wx.setStorageSync('infordata',infordata )
    }

    this.setData({
      infordata,
      infordatalength
    })
  },
  // 选项答案时选中后的效果
  change(e) {
    // console.log(this.doneinfor);
    // 打印点击的信息 console.log(e)
    // 记录正确的题目数
    let truenum = this.data.truenum;
    // 记录做的题目数
    let writenum = this.data.writenum;
    // 题目下标
    let infordataindex = this.data.infordataindex;
    // 获取点击的那个
    const options = e.currentTarget.dataset.checked;
    const answeroption = options;
    // 获取题目的答案
    const infordataanswer = this.data.infordata[infordataindex].answer;
    // console.log(infordataanswer);
    let times = this.data.times;
    // 记录做过这个题目

    // 打印选的答案 console.log(this.data.option);
    if (this.data.answeroption < 0) {
      this.setData({
        option: options,
        show: false
      })
      //  选错了的操作
      if (options != infordataanswer) {
        times = 2;
        this.setData({
          times,
          show: true
        })
        wx.showToast({
          title: '选错了看解析吧',
          icon: 'error',
          duration: 500,
          mask: true
        })
      } else {
        // 选对了的操作
        wx.showToast({
          title: '你很不错哦',
          icon: 'success',
          duration: 500,
          mask: true
        })

        if (truenum <= this.data.infordatalength && times == 1) {
          truenum++;

        }


        //  对做正确了题目进行下一题
        if (infordataindex <= this.data.infordatalength) {
          let infordataindex = ++this.data.infordataindex

          this.setData({
            option: 0
          })

          writenum = infordataindex;
          times = 1;
          // console.log("infordataindex==>", infordataindex)
          this.setData({
            infordataindex,
            writenum,
            times
          })
        }
      }
      wx.setStorageSync('truenum', truenum)
      wx.setStorageSync('writenum', writenum)
      this.setData({
        writenum,
        truenum,
      })

    } else {
      wx.showToast({
        title: '你已经选咯呢',
        icon: 'error',
        duration: 500,
        mask: true
      })
    }

  },
  // 显示解析
  analysis_show() {
    //  console.log(this.data.show);
    let show = !this.data.show;
    this.setData({
      show
    })

  },
  // 上一题
  upinfordata() {
    let isCollect = false;
    let iserrorcollect = false;
    if (this.data.infordataindex) {
      let infordataindex = --this.data.infordataindex;
      this.setData({
        infordataindex,
        isCollect,
        iserrorcollect,
        stopnext: false
      })
      this.change();

    } else {
      wx.showToast({
        title: '已经是第一题了',
        icon: 'error',
        duration: 500,
        mask: true
      })
    }
  },
  // 下一题
  downinfordata() {
    let isCollect = false;
    let iserrorcollect = false;

    if (this.data.infordataindex <= this.data.infordatalength) {
      let infordataindex = ++this.data.infordataindex;
      this.setData({
        infordataindex,
        isCollect,
        iserrorcollect
      })
      if (this.data.infordataindex == this.data.infordatalength) {
        this.setData({
          stopnext: true
        })
      }
      if(this.data.infordatalength==0){
        this.setData({
          stopnext:true
        })
      }
      this.change()
    } else {
      wx.showToast({
        title: '已经是最后第一题了',
        icon: 'error',
        duration: 500,
        mask: true
      })
    }

  },
  // 收藏
  collectinfor() {
    let isCollect = false;
    let collectinfor = wx.getStorageSync('collectinfor') || [];
    let index = collectinfor.findIndex(v => v.id == this.data.infordata[this.data.infordataindex].id);

    if (index !== -1) {
      collectinfor.splice(index);
      isCollect = false
      wx.showToast({
        title: '取消收藏',
        icon: 'error',
        duration: 500,
        mask: true
      })
    } else {
      collectinfor.push(this.data.infordata[this.data.infordataindex])
      isCollect = true
      wx.showToast({
        title: '成功收藏',
        icon: 'success',
        duration: 500,
        mask: true
      })
    }
    wx.setStorageSync('collectinfor', collectinfor)
    this.setData({
      isCollect
    })
  },
  // 加入错题
  errorinfor() {
    let iserrorcollect = false;
    let errorinfor = wx.getStorageSync('errorinfor') || [];
    let index = errorinfor.findIndex(v => v.id == this.data.infordata[this.data.infordataindex].id);

    if (index !== -1) {
      errorinfor.splice(index);
      iserrorcollect = false
      wx.showToast({
        title: '取消加入',
        icon: 'error',
        duration: 500,
        mask: true
      })
    } else {
      errorinfor.push(this.data.infordata[this.data.infordataindex])
      iserrorcollect = true
      wx.showToast({
        title: '成功加入',
        icon: 'success',
        duration: 500,
        mask: true
      })
    }
    wx.setStorageSync('errorinfor', errorinfor)
    this.setData({
      iserrorcollect
    })
  }
})